/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mask;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author ESCRITORIO
 */     
public class FixedLengthNumberDocument extends PlainDocument  
{  
    private int limite = 0;  
  
    public FixedLengthNumberDocument(int l){  
        this.limite = l;  
    }  
  
    public void insertString(int offs, String str, AttributeSet a)  
               throws BadLocationException {  
  
        int sobra = limite - getLength();  
        int comprimento =  ( sobra > str.length() ) ? str.length() : sobra;  
        
        for( int i = 0; i < str.length(); i++ )
            if( Character.isDigit( str.charAt( i ) ) == false)
                return;
        
        super.insertString(offs, str.substring(0, comprimento), a);  
    }   
} 
